import 'dart:async';

import 'package:get/get.dart';

import 'xin_he_group_purchase_order_detail_state.dart';

class XinHeGroupPurchaseOrderDetailLogic extends GetxController {
  final XinHeGroupPurchaseOrderDetailState state = XinHeGroupPurchaseOrderDetailState();

  Timer? _snapUpTimer;
  int secondsRemaining = 610; // 倒计时的总秒数
  RxString snapUpHour = '00'.obs;
  RxString snapUpMinute = '00'.obs;
  RxString snapUpSecond = '00'.obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    startCountdown();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    if(_snapUpTimer != null){
      _snapUpTimer?.cancel();
      _snapUpTimer = null;
    }
  }
  void startCountdown() {
    _snapUpTimer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
      if (secondsRemaining > 0) {
        secondsRemaining--;
        int hours = secondsRemaining ~/ 3600;
        int minutes = (secondsRemaining % 3600) ~/ 60;
        int remainingSeconds = secondsRemaining % 60;

        String hoursStr = (hours < 10) ? '0$hours' : hours.toString();
        String minutesStr = (minutes < 10) ? '0$minutes' : minutes.toString();
        String secondsStr = (remainingSeconds < 10) ? '0$remainingSeconds' : remainingSeconds.toString();

        snapUpHour.value = hoursStr;
        snapUpMinute.value = minutesStr;
        snapUpSecond.value = secondsStr;
      } else {
        timer.cancel();
        snapUpHour.value = '00';
        snapUpMinute.value = '00';
        snapUpSecond.value = '00';
      }
    });
  }
}
